package javase.javase02.networkprograming;

import java.io.*;
import java.net.*;

/**
 * @author: Gan
 * @date 2021/4/23 0023 20:10
 * @description: 2.客户端上传图片到服务器
 **/
public class ClientWork2 {
    public static void main(String[] args) throws IOException {
        //1.创建客户端Socket对象
        Socket socket = new Socket("127.0.0.1", 3345);
        //2.读取要上传的图片
        FileInputStream fis = new FileInputStream(
                "D:\\BaiduNetdiskDownload\\win10png\\timg.jpg");
        //3.将图片发送到服务器端
        OutputStream out = socket.getOutputStream();
        byte[] bytes = new byte[1024];
        int readCount = 0;
        while ((readCount = fis.read(bytes)) != -1) {
            out.write(bytes,0,readCount);
        }
        socket.shutdownOutput();
        //4.接收服务器端返回的数据。
        InputStream in = socket.getInputStream();
        byte[] bytes1 = new byte[1024];
        int readCount2 = in.read(bytes1);
        System.out.println(new String(bytes1,0,readCount2));
        out.close();
        socket.close();
    }
}
